create table #list (db varchar(30), name varchar(30), id tinyint, fname varchar(100)) insert #list exec sp_msforeachdb 'use ? select db_name(), name, fileid, filename from sysfiles' select * from #list select ' print ''--' + d.name + ''' declare @dt datetime set @dt = getdate() if db_id(''' + d.name + ''') is not null drop database ' + d.name + ' restore database ' + d.name + ' from disk = ''\\NEWZ\g$\' + d.name + '.bak'' with recovery, move ''' + rtrim(l1.name) + ''' to ''' + replace(rtrim(l1.fname),'mssql7','mssql') + ''', move ''' + rtrim(l2.name) + ''' to ''' + replace(rtrim(l2.fname),'mssql7','mssql') + ''' select datediff(s,@dt,getdate()) as [seconds to restore] GO' from sysdatabases d inner join #list l1 on d.name = l1.db inner join #list l2 on d.name = l2.db where d.name not in ('master','model','msdb','tempdb') and d.name not like 'not_used%' and l1.id = 1 and l2.id = 2